using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;


public partial class SPTypeToMessageTypeRelationship : System.Web.UI.Page
{
    public string function;
    AdminServices.AdminServices proxy;
    AdminServices.MessageTypeSearchRequest messageTypeSearchRequest;
    AdminServices.ServiceProviderTypeSearchRequest serviceProviderTypeSearchRequest;
    AdminServices.HSDPrincipal currentPrincipal;
    List<AdminServices.ServiceProviderTypeMessageTypeLink> serviceProviderTypeMessageTypeLinkList;  

    protected void Page_Load(object sender, EventArgs e)
    {
 
        if (Request["function"] != null)
        {
            Session["function"] = Request["function"];
        }
        function = Session["function"].ToString();

        if (Session["currentPrincipal"] == null)
        {
            Session["currentPrincipal"] = GetPrincipal();
        }

        currentPrincipal = (AdminServices.HSDPrincipal)Session["currentPrincipal"];

        proxy = new AdminServices.AdminServices();

        if (!IsPostBack)
        {
           Session["serviceProviderTypeMessageTypeLinkList"] = null;
           setControls();
           searchAndBindMessageTypeGrid(0);         
        } 
        serviceProviderTypeMessageTypeLinkList = (List<AdminServices.ServiceProviderTypeMessageTypeLink>)Session["serviceProviderTypeMessageTypeLinkList"];

    }

    /// <summary>
    /// This function load the grid for the relevent entity
    /// </summary>
    private void searchAndBindMessageTypeGrid(int pageIndex)
    {
        messageTypeSearchRequest = new AdminServices.MessageTypeSearchRequest();
        messageTypeSearchRequest.Description = txtDescription.Text;
        messageTypeSearchRequest.Version = txtVersion.Text;
        AdminServices.MessageType[] results = proxy.MessageTypeSearch(currentPrincipal, messageTypeSearchRequest);
        gvMessageType.DataSource = results;
        gvMessageType.PageIndex = pageIndex;
        gvMessageType.DataBind();
    }

    private void bindRelationshipGrid(int pageNo) 
    {
        gvRelationship.DataSource = serviceProviderTypeMessageTypeLinkList;
        gvRelationship.PageIndex = pageNo;
        gvRelationship.DataBind();
    }

    private void addToList()
    {
        //Loop through GridView Items
        if (serviceProviderTypeMessageTypeLinkList == null)
        {
            serviceProviderTypeMessageTypeLinkList = new List<AdminServices.ServiceProviderTypeMessageTypeLink>();
        }
        foreach (GridViewRow gvr in gvMessageType.Rows)
        {
            //Retrieve records that are checked
            CheckBox checkBox = (CheckBox)gvr.FindControl("chkSelect");
            if (checkBox != null)
            {
                if (checkBox.Checked)
                {
                    AdminServices.ServiceProviderTypeMessageTypeLink link = new AdminServices.ServiceProviderTypeMessageTypeLink();

                    link.MessageTypeID = Convert.ToInt32(gvMessageType.DataKeys[gvr.RowIndex].Value);
                    link.Description = gvr.Cells[1].Text;
                    link.SourceDisplayName = ddlSource.SelectedItem.Text;
                    link.SourceServiceProviderTypeID = Convert.ToInt32(ddlSource.SelectedValue);
                    link.DestinationDisplayName = ddlDestination.SelectedItem.Text;
                    link.DestinationServiceProviderTypeID = Convert.ToInt32(ddlDestination.SelectedValue);
                    serviceProviderTypeMessageTypeLinkList.Add(link);                    
                    checkBox.Checked = false;
                }
            }
        }
        Session["serviceProviderTypeMessageTypeLinkList"] = serviceProviderTypeMessageTypeLinkList;
        bindRelationshipGrid(int.Parse(tbPage2.Text));
    }


    ///// <summary>
    ///// This function set controls for the relevant entity
    ///// </summary>
    private void setControls()
    {
        //AdminServices.ServiceProviderType spt = new AdminServices.ServiceProviderType();
        //spt.ServiceProviderTypeID = 0;
        //spt.DisplayName = "~Please select a SPT~";
        serviceProviderTypeSearchRequest = new AdminServices.ServiceProviderTypeSearchRequest();
        AdminServices.ServiceProviderType[] results = proxy.ServiceProviderTypeSearch(currentPrincipal, serviceProviderTypeSearchRequest);
        
        ddlDestination.DataSource = results;
        ddlDestination.DataBind();
        ddlDestination.Items.Add(new ListItem("~ Please select a SPT ~", "0"));
        ddlDestination.SelectedValue = "0";

        ddlSource.DataSource = results;
        ddlSource.DataBind(); 
        ddlSource.Items.Add(new ListItem("~ Please select a SPT ~", "0"));
        ddlSource.SelectedValue = "0";

        if (function != "Delete")
        {
            gvRelationship.Columns[0].Visible = false;            
        }
        if (function == "Add")
        {
            btnAddToList.Visible = true;
            gvMessageType.Visible = true;
            btnSearchWithSPs.Visible = false;
            btnSearch.Visible = true;
        }
        //setRelationshipGrid();

        btnSave.Text = string.Format("{0} Relationship", function);

    }
    private void setRelationshipGrid()
    {
        if (serviceProviderTypeMessageTypeLinkList == null && function == "Add")
        {
            gvRelationship.Visible = false;            
        }
        else
        {
            gvRelationship.Visible = true;
            if (function != "Search")
            {
                btnCancel.Visible = true;
                btnSave.Visible = true;
            }
        }
        gvRelationship.EmptyDataText = string.Format("No Relationships to {0}", function);
    }

    private void saveRelationship()
    {
        if (serviceProviderTypeMessageTypeLinkList != null)
        {
            //Change list to array of objects
            AdminServices.ServiceProviderTypeMessageTypeLink[] ServiceProviderTypeMessageTypeLinks = new AdminServices.ServiceProviderTypeMessageTypeLink[serviceProviderTypeMessageTypeLinkList.Count - 1];
            ServiceProviderTypeMessageTypeLinks = (AdminServices.ServiceProviderTypeMessageTypeLink[])serviceProviderTypeMessageTypeLinkList.ToArray();
            proxy.ServiceProviderTypeMessageTypeLinksInsert(currentPrincipal, ServiceProviderTypeMessageTypeLinks);
            clearRelationshipGrid();
        }            
    }

    private void clearRelationshipGrid()
    {
        serviceProviderTypeMessageTypeLinkList = null;
        Session["serviceProviderTypeMessageTypeLinkList"] = null;
        tbPage2.Text = "0";
        gvRelationship.DataBind();
    }

    private AdminServices.HSDPrincipal GetPrincipal()
    {
        proxy = new AdminServices.AdminServices();
        return proxy.ValidateUser("me", "!password01");
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        tbPage.Text = "0";
        searchAndBindMessageTypeGrid(int.Parse(tbPage.Text));       
    }

    private void searchAndBindRelationshipGrid(int pageNo)
    {
        messageTypeSearchRequest = new AdminServices.MessageTypeSearchRequest();
        messageTypeSearchRequest.Description = txtDescription.Text;
        messageTypeSearchRequest.Version = txtVersion.Text;
        if (ddlDestination.SelectedValue != "0" && ddlSource.SelectedValue != "0")
        {
            messageTypeSearchRequest.DestinationServiceProviderTypeID = ddlDestination.SelectedValue;
            messageTypeSearchRequest.SourceServiceProviderTypeID = ddlSource.SelectedValue;
        }
        AdminServices.MessageType[] results = proxy.MessageTypeSearch(currentPrincipal, messageTypeSearchRequest);
        gvRelationship.DataSource = results;
        gvRelationship.PageIndex = pageNo;
        gvRelationship.DataBind();
        if (results != null && function == "Delete")
        {
            btnSave.Visible = true;
            btnCancel.Visible = true;
        }
    }

    private void deleteRelationship()
    {
        //Loop through GridView Items
        if (serviceProviderTypeMessageTypeLinkList == null)
        {
            serviceProviderTypeMessageTypeLinkList = new List<AdminServices.ServiceProviderTypeMessageTypeLink>();
        }
        foreach (GridViewRow gvr in gvRelationship.Rows)
        {
            //Retrieve records that are checked
            CheckBox checkBox = (CheckBox)gvr.FindControl("chkSelect");
            if (checkBox != null)
            {
                if (checkBox.Checked)
                {
                    AdminServices.ServiceProviderTypeMessageTypeLink link = new AdminServices.ServiceProviderTypeMessageTypeLink();
                    link.ServiceProviderTypeMessageTypeID = Convert.ToInt32(gvRelationship.DataKeys[gvr.RowIndex].Value); 
                    serviceProviderTypeMessageTypeLinkList.Add(link);
                }
            }
        }

        if (serviceProviderTypeMessageTypeLinkList != null)
        {
            //Change list to array of objects
            AdminServices.ServiceProviderTypeMessageTypeLink[] ServiceProviderTypeMessageTypeLinks = new AdminServices.ServiceProviderTypeMessageTypeLink[serviceProviderTypeMessageTypeLinkList.Count - 1];
            ServiceProviderTypeMessageTypeLinks = (AdminServices.ServiceProviderTypeMessageTypeLink[])serviceProviderTypeMessageTypeLinkList.ToArray();
            proxy.ServiceProviderTypeMessageTypeLinksDelete(currentPrincipal, ServiceProviderTypeMessageTypeLinks);
            searchAndBindRelationshipGrid(int.Parse(tbPage2.Text));
        } 

    }

    protected void btnAddToList_Click(object sender, EventArgs e)
    {
        addToList();
        setRelationshipGrid();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (function == "Add")
            saveRelationship();
        else
            deleteRelationship();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        clearRelationshipGrid();
    }
    protected void btnSearchWithSPs_Click(object sender, EventArgs e)
    {
        tbPage2.Text = "0";
        searchAndBindRelationshipGrid(int.Parse(tbPage2.Text));
        setRelationshipGrid();
    }
    protected void gvMessageType_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {        
        searchAndBindMessageTypeGrid(e.NewPageIndex);
        tbPage.Text = e.NewPageIndex.ToString();   
    }
    protected void gvRelationship_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (function == "Search")
            searchAndBindRelationshipGrid(e.NewPageIndex);
        else
            bindRelationshipGrid(e.NewPageIndex);

        tbPage2.Text = e.NewPageIndex.ToString();   
    }
}


